<?php
#=========================================================================================
#	Filename: parseCountryCode.class.php
#	Note	: parseCountryCode Class For parse xml
#	Version : 1.0
#	Author  : Peter Zhang <peter.zhang@achievo.com>
#	Update  : 2010-05-11
#======================================================================================== 

require_once 'TVB/ObjectRecord/all.php';
require_once 'TVB/Config/ConfigReader.php';

class parseCountryCode{
	
	/*
	 * e.g array('Africa'=> array('Africa'=> array('AO'=>'Angola','BF'=>' Burkina Faso')));
	 * 
	 */
	public static function parseXml($configFile,$exceptFlag=FALSE){
		
		$retrun = array();
		if(file_exists($configFile)){
			
			//$configArray = ConfigReaderFactory::getInstance()->getConfigReader($configFile)->getConfig();
			$configArray = simplexml_load_file($configFile);
			//return $configArray;
			foreach($configArray->region as $regionKey => $regionValue){
				
				//change to string
				$regionVar = (string)$regionValue->attributes()->name;
				$countryArray = array();
				foreach ($regionValue->country as $ccKey => $ccValue){
					
					//change to string
					$code = (string)$ccValue->attributes()->code;
					$name = (string)$ccValue->attributes()->name;
					$countryFlag = (string)$ccValue->attributes()->except;
					if($exceptFlag)
						$countryArray[$code] = array($name,$countryFlag);
					else 
						$countryArray[$code] = $name;
				}
				//two region keys
				$return[$regionVar][$regionVar]  = $countryArray;
			}
			
			return $return;
			
		}else{
			
			//Error: file not exists,or file path is wrong
			return false;	
		}
	}
}

/*
 * test
echo "<pre>";
$xmlPath = 'countryCode.xml';
print_r(parseCountryCode::parseXml($xmlPath,true));
 */
//print_r(parseCountryCode::parseXml($xmlPath));
?>